package com.wwwarehouse.taskcenter.fragment.job_point.step5;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.wwwarehouse.common.fragment.CommonBasePagerFragment;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.taskcenter.R;
import com.wwwarehouse.taskcenter.bean.job_point.AddJobPointStep5LookItemMesResponseBean;
import com.wwwarehouse.taskcenter.eventbus_event.JobPointSetnumberEvent;

import de.greenrobot.event.EventBus;

/**
 * @创建者 chenchao.
 * @创建时间 2017/10/25 18:53
 * @描述 ${TODO}
 */

public class ChooseJobUnitSetnumberpagerFragment5 extends CommonBasePagerFragment implements View.OnClickListener {

    private Button mBtSave;
    public static final int REQUEST_CODE = 10004;
    private AddJobPointStep5LookItemMesResponseBean selectBean;

    private String jobPointUkid;

    @Override
    public void setTitle() {
        if (peekFragment() instanceof ChooseJobUnitSetnumberpagerFragment5) {
            mActivity.setTitle("" + getString(R.string.new_task_step5_judege_select_unit));
        }
    }

    @Override
    public void requestDatas() {
        super.requestDatas();
        EventBus.getDefault().register(this);

        jobPointUkid = getArguments().getString("jobPointUkid");

        showConfirmButton();
        setConfirmBtnText("" + getString(R.string.new_task_save));
        mBtSave = (Button) getConfirmBtn();
        mBtSave.setOnClickListener(this);
        initPager();
    }

    @Override
    public void onDownPull() {

    }

    private void initPager() {
        selectBean = getArguments().getParcelable("selectBean");
        Bundle budle = new Bundle();
        try {
            setData(selectBean.getList().size(), 10, "com/wwwarehouse/taskcenter/fragment/job_point/step5/ChooseJobUnitSetnumberDetailFragment5", budle, selectBean.getList());
        } catch (Exception e) {
            e.printStackTrace();
            LogUtils.showErrLog("error:"+e.getMessage());
        }
    }

    @Override
    public void onRefreshListener() {
    }

    @Override
    public void onLoadSuccess(String s, int i) {
    }

    @Override
    public void onLoadError(String s) {

    }

    @Override
    public void onClick(View v) {
        if (v == mBtSave) {
            Bundle bundle = new Bundle();
            bundle.putString("jobPointUkid","" + jobPointUkid);
            bundle.putParcelable("data",selectBean);
            ChooseJobUnitShowallMessageFragment fragment5 = new ChooseJobUnitShowallMessageFragment();
            fragment5.setArguments(bundle);
            pushFragment(fragment5);
        }
    }

    public void onEventMainThread(JobPointSetnumberEvent event) {
        int i = 0,count = 0;
        AddJobPointStep5LookItemMesResponseBean.ListBean listbean;
        for(i = 0 ; i < selectBean.getList().size(); i ++){
            listbean = selectBean.getList().get(i);
            if(listbean.getCount() == 0 ){
                count ++;
            }
        }
        if(selectBean !=null && selectBean.getList() !=null ){
            if(count == selectBean.getList().size()){
                mBtSave.setEnabled(false);
            }else{
                mBtSave.setEnabled(true);
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
